package Principal;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import com.mxrck.autocompleter.TextAutoCompleter;
import java.util.logging.Level;
import preparedstatement.DebugLevel;
import preparedstatement.StatementFactory;
/**
 * Clase donde muestra los gastos que se haran par aun proyecto.
 * @author pato
 */
public class gastos extends javax.swing.JDialog {
    TextAutoCompleter textAutoCompleter;
    PreparedStatement ps;
    ResultSet rs;
    local_connection conexion;
    String nombre;
    int code;
    Calendar Cal= Calendar.getInstance(); 
    String fec= Cal.get(Cal.DATE)+"/"+(Cal.get(Cal.MONTH)+1)+"/"+Cal.get(Cal.YEAR)+" "+Cal.get(Cal.HOUR_OF_DAY)+":"+Cal.get(Cal.MINUTE)+":"+Cal.get(Cal.SECOND); 
    
    /**
     * Creates new form gastos
     * @param parent 
     * @param modal 
     * @param cd 
     */
    public gastos(java.awt.Frame parent, boolean modal,int cd) {
        super(parent, modal);
        initComponents();
        code = cd;
        textAutoCompleter = new TextAutoCompleter(JTFproyecto);
        textAutoCompleter.setMode(0);
        textAutoCompleter.setCaseSensitive(false);
        cantidad.setDocument(new LimitadorCaracteresFloat());
         try 
         {
            local_connection.cn = local_connection.getconnect();
         } 
         catch (Exception e) 
         {
         }
        
        try
        {
            String name = "nombre||' '||paterno||' '||materno";
            ps = (PreparedStatement) cache_connection.cn.prepareStatement("Select "+name+" from personas where id_persona = ?");
            ps.setInt(1,code);
            rs = ps.executeQuery();
            rs.next();
            if (rs.getString(1).length() >= 15) 
            {
                    nombre = rs.getString(1).substring(0, 15) + "...";
            } 
            else 
            {
                    nombre = rs.getString(1);
            }
         } 
         catch(Exception e)
         {
            System.out.println(e.getMessage());
         }
        
        JLencargado.setText(nombre);
        JLfecha.setText(fec);
        cargarDatos();
        
    }
    
    void cargarDatos()
    {
        try{ ps = (PreparedStatement) cache_connection.cn.prepareStatement("select proyecto from gastos where sucursal = ?");
            ps.setInt(1,VentanaPrincipal.sucursal);
            rs = ps.executeQuery();
            while (rs.next()) {
                textAutoCompleter.addItem(rs.getString(1));
            }}
        catch(Exception e)
         {
            System.out.println(e.getMessage());
         }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cantidad = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        concepto = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        JLfecha = new javax.swing.JLabel();
        JLencargado = new javax.swing.JLabel();
        JLdepartamento = new javax.swing.JLabel();
        JTFdepartamento = new javax.swing.JTextField();
        JLproyecto = new javax.swing.JLabel();
        JTFproyecto = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        JCBfactura = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        JCBnota = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        responsable = new javax.swing.JTextField();

        jLabel3.setText("jLabel3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Cantidad: $");

        concepto.setColumns(20);
        concepto.setRows(5);
        jScrollPane1.setViewportView(concepto);

        jLabel2.setText("Concepto:");

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Aceptar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        JLfecha.setText("Fecha");

        JLencargado.setText("Encargado");

        JLdepartamento.setText("Departamento:");

        JLproyecto.setText("Proyecto:");

        jLabel4.setText("Factura");

        JCBfactura.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No", "Si" }));

        jLabel5.setText("Nota:");

        JCBnota.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No", "Si" }));

        jLabel6.setText("Responsable:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(JLencargado, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(JLfecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(JLproyecto)
                        .addGap(30, 30, 30)
                        .addComponent(JTFproyecto))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(229, 229, 229))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(JCBnota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(21, 21, 21)
                                .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(JCBfactura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JLdepartamento)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(responsable)
                            .addComponent(JTFdepartamento))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JLfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JLencargado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(responsable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JLdepartamento)
                    .addComponent(JTFdepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JLproyecto)
                    .addComponent(JTFproyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(JCBfactura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(JCBnota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
if(!cantidad.getText().equals(""))       
         try {
            ps = StatementFactory.getStatement(local_connection.cn,"Insert into gastos(nombre,departamento,proyecto,cantidad,concepto,factura,nota,usuario,sucursal,caja) values(?,?,?,?,?,?,?,?,?,?)",DebugLevel.ON);
            ps.setString(1,responsable.getText());
            ps.setString(2, JTFdepartamento.getText());
            ps.setString(3, JTFproyecto.getText());
            ps.setString(4,cantidad.getText());
            ps.setString(5,concepto.getText());
            ps.setInt(6, JCBfactura.getSelectedIndex());
            ps.setInt(7, JCBnota.getSelectedIndex());
            ps.setInt(8,code);
            ps.setInt(9,VentanaPrincipal.sucursal);
            ps.setInt(10,VentanaPrincipal.caja);
            VentanaPrincipal.guardarCache(ps, "gastos", true);
          //  ps = (PreparedStatement) local_connection.cn.prepareStatement(ps.toString());
            ps.execute();
            this.dispose();
        } catch (Exception e) {
        VentanaPrincipal.logger.log(Level.SEVERE, null, e);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed
   
    public class LimitadorCaracteresFloat extends PlainDocument {

        /**
         * Método al que llama el editor cada vez que se intenta insertar
         * caracteres. Sólo debemos verificar arg1, que es la cadena que se
         * quiere insertar en el JTextField
         */
        @Override
        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException {
            for (int i = 0; i < arg1.length(); i++) // si no es digit, volvemos
            {
                if (!(Character.isDigit(arg1.charAt(i)) || arg1.charAt(i) == '.')) {
                    return;
                }
            }

            // Si todos son digit, insertamos el texto en el JTextField
            super.insertString(arg0, arg1, arg2);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox JCBfactura;
    private javax.swing.JComboBox JCBnota;
    private javax.swing.JLabel JLdepartamento;
    private javax.swing.JLabel JLencargado;
    private javax.swing.JLabel JLfecha;
    private javax.swing.JLabel JLproyecto;
    private javax.swing.JTextField JTFdepartamento;
    private javax.swing.JTextField JTFproyecto;
    private javax.swing.JTextField cantidad;
    private javax.swing.JTextArea concepto;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField responsable;
    // End of variables declaration//GEN-END:variables
}
